package com.zlx.sharelive.activity;

import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.zlx.module_base.base_ac.BaseAc;
import com.zlx.module_base.base_api.res_data.HomeBannerBean;
import com.zlx.module_base.base_api.util.ApiUtil;
import com.zlx.module_network.api1.livedata.BaseObserver;
import com.zlx.module_network.api1.livedata.BaseObserverCallBack;
import com.zlx.module_network.bean.ApiResponse;
import com.zlx.sharelive.R;
import com.zlx.sharelive.utils.GlideImgManager;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 广告页
 */
public class AdvertisingAc extends BaseAc {

    @BindView(R.id.iv_image)
    ImageView iv_image;
    @BindView(R.id.tv_time)
    TextView tv_time;

    private int recLen = 5;//跳过倒计时提示5秒
    Timer timer = new Timer();
    private Handler handler;
    private Runnable runnable;
    private String url = "";

    @Override
    protected int getLayoutId() {
        return R.layout.ac_advertising;
    }


    @Override
    public void initViews() {
        super.initViews();

        getHomeRollAnnouncement();

        timer.schedule(task, 1000, 1000);//等待时间一秒，停顿时间一秒
        initialization();
    }

    private void getHomeRollAnnouncement() {
        ApiUtil.getHomeApi().getHomeBannerList("4").observe(this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<HomeBannerBean>>() {
            @Override
            public void onSuccess(ApiResponse<HomeBannerBean> data) {
                if (null != data) {
                    if (null != data.getData()) {
                        if (null != data.getData().getInfo()) {
                            if (data.getData().getInfo().size() > 0) {
                                url = data.getData().getInfo().get(0).getLink();
                                GlideImgManager.loadImage2(AdvertisingAc.this, data.getData().getInfo().get(0).getImg(), iv_image);
                            }
                        }
                    }
                }
            }
        }));
    }

    private void initialization() {
        handler = new Handler();
        handler.postDelayed(runnable = new Runnable() {
            @Override
            public void run() {
//                //从闪屏界面跳转到首界面
//                if (null != SPUtil.getObjectFromShare(AdvertisingAc.this,
//                        Constants.USERINFO)) {
                startActivity(new Intent(AdvertisingAc.this, MainAc.class));
//                } else {
//                    startActivity(new Intent(AdvertisingAc.this, LoginAc.class));
//                }
                finish();
            }
        }, 5000);//延迟5S后发送handler信息
    }

    TimerTask task = new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() { // UI thread
                @Override
                public void run() {
                    recLen--;
                    tv_time.setText(recLen + "s");
                    if (recLen < 0) {
                        timer.cancel();
                    }
                }
            });
        }
    };

    @OnClick({R.id.ll_leapfrog, R.id.tv_enter})
    public void setOnClick(View view) {
        switch (view.getId()) {
            case R.id.ll_leapfrog:
                startActivity(new Intent(AdvertisingAc.this, MainAc.class));
                finish();
                if (runnable != null) {
                    handler.removeCallbacks(runnable);
                }
                break;
            case R.id.tv_enter:
                startActivity(new Intent(AdvertisingAc.this,
                        HomeLinkAc.class)
                        .putExtra("url", url)
                        .putExtra("type", "1"));
                finish();
                if (runnable != null) {
                    handler.removeCallbacks(runnable);
                }
                break;
        }
    }

}
